package com.bestom.bluetoothlock.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.alibaba.fastjson.JSON;
import com.bestom.bluetoothlock.entity.DeviceInfo;
import com.bestom.bluetoothlock.entity.UserInfo;

import java.util.Locale;
import java.util.Set;

public class CurrencyDataManage {
    public static final String OPEN_LOG = "open_log";
    private static final String TAG = "CurrencyDataManage";
    private static CurrencyDataManage instance;
    private Context context;
    private SharedPreferences.Editor editor;
    private SharedPreferences sharedPreferences;

    private CurrencyDataManage(Context context2) {
        this.context = context2;
        this.sharedPreferences = this.context.getSharedPreferences("favoured_ring", 0);
        this.editor = this.sharedPreferences.edit();
    }

    private CurrencyDataManage() {
    }

    public static CurrencyDataManage getInstance() {
        if (instance == null) {
            synchronized (CurrencyDataManage.class) {
                if (instance == null) {
                    instance = new CurrencyDataManage();
                }
            }
        }
        return instance;
    }

    public void init(Context context2) {
        this.context = context2;
        this.sharedPreferences = this.context.getSharedPreferences("favoured_ring", 0);
        this.editor = this.sharedPreferences.edit();
    }

    public SharedPreferences getSharedPreferences() {
        return this.sharedPreferences;
    }

    public boolean getLogSwitch() {
        return this.sharedPreferences.getBoolean(OPEN_LOG, false);
    }

    public void setLogSwitch(boolean z) {
        setValue(OPEN_LOG, Boolean.valueOf(z));
    }

    public DeviceInfo getCurrentBleDevice() {
        DeviceInfo deviceInfo = (DeviceInfo) JSON.parseObject(this.sharedPreferences.getString("ble_device", ""), DeviceInfo.class);
        return deviceInfo == null ? new DeviceInfo() : deviceInfo;
    }

    public void setCurrenBleDevice(DeviceInfo deviceInfo) {
        if (deviceInfo != null) {
            setValue("ble_device", JSON.toJSONString(deviceInfo));
        }
    }

    public String getLastTimeLoginPhone() {
        return this.sharedPreferences.getString("login_phone", "");
    }

    public void setLastTimeLoginPhone(String str) {
        if (str == null) {
            setValue("login_phone", "");
        } else {
            setValue("login_phone", str);
        }
    }

    public UserInfo getLoginUserInfo() {
        UserInfo userInfo = (UserInfo) JSON.parseObject(this.sharedPreferences.getString("login_user_info", ""), UserInfo.class);
        if (userInfo == null) {
            return userInfo;
        }
        if (userInfo.getLoginNumber() == null || userInfo.getLoginNumber().length() < 1) {
            return null;
        }
        return userInfo;
    }

    public void setLoginUserInfo(UserInfo userInfo) {
        if (userInfo == null) {
            setValue("login_user_info", (Object) null);
        } else {
            setValue("login_user_info", JSON.toJSONString(userInfo));
        }
    }

    public void setUserSettingConfig(DeviceInfo deviceInfo) {
        if (deviceInfo != null) {
            setValue("ble_device", JSON.toJSONString(deviceInfo));
        }
    }

    public DeviceInfo getUserSettingConfig() {
        DeviceInfo deviceInfo = (DeviceInfo) JSON.parseObject(this.sharedPreferences.getString("ble_device", ""), DeviceInfo.class);
        return deviceInfo == null ? new DeviceInfo() : deviceInfo;
    }

    public void setDeviceBattery(int i) {
        setValue("device_battery", Integer.valueOf(i));
    }

    public int getDeviceBattery() {
        return this.sharedPreferences.getInt("device_battery", 0);
    }

    public void setIsSavaLOG(boolean z) {
        setValue("sava_log", Boolean.valueOf(z));
    }

    public boolean isSavaLOG() {
        return this.sharedPreferences.getBoolean("sava_log", false);
    }

    public void setCurrentLanguage(String str) {
        setValue("current_language", str);
    }

    public String getCurrentLanguage() {
        SharedPreferences sharedPreferences2 = this.sharedPreferences;
        return sharedPreferences2.getString("current_language", Locale.getDefault().getLanguage() + "-" + Locale.getDefault().getCountry());
    }

    public void setCurrentPicture(String str) {
        setValue("current_picture", str);
    }

    public String getCurrentPicture() {
        return this.sharedPreferences.getString("current_picture", "");
    }

    public void setValue(String str, Object obj) {
        if (obj instanceof Integer) {
            this.editor.putInt(str, ((Integer) obj).intValue());
        } else if (obj instanceof String) {
            this.editor.putString(str, (String) obj);
        } else if (obj instanceof Boolean) {
            this.editor.putBoolean(str, ((Boolean) obj).booleanValue());
        } else if (obj instanceof Float) {
            this.editor.putFloat(str, ((Float) obj).floatValue());
        } else if (obj instanceof Long) {
            this.editor.putLong(str, ((Long) obj).longValue());
        } else if (obj instanceof Set) {
            this.editor.putStringSet(str, (Set) obj);
        } else {
            return;
        }
        this.editor.commit();
    }
}
